package com.wensi.lib_voice.manager

import android.content.Context
import android.util.Log
import com.baidu.speech.EventListener
import com.baidu.speech.asr.SpeechConstant
import com.wensi.lib_voice.asr.VoiceAsr
import com.wensi.lib_voice.impl.OnAsrResultListener
import com.wensi.lib_voice.tts.VoiceTTS
import com.wensi.lib_voice.wakeup.VoiceWakeup
import org.json.JSONObject

/**
 * 语音管理类 - 创建的一个包装类：专门针对该语音sdk创建
 */
object VoiceManager : EventListener {
    const val VOICE_APP_ID = "24488474"
    const val VOICE_API_KEY = "CK82YnDBxRK61yEB7yI2cn6r"
    const val VOICE_SECRET_KEY = "QjLMjRbYLDFD4vuT394dxNKlbuhvM6Il"

    private val TAG = VoiceManager::class.simpleName
    private lateinit var asrListener: OnAsrResultListener
    fun initManager (context: Context, asrListener: OnAsrResultListener) {
        this.asrListener = asrListener
//        WordTools.initTools(context) // 初始化默认词条
        VoiceTTS.initTTS(context)
        VoiceWakeup.initWakeup(context, this)
        VoiceAsr.initAsr(context, this)
    }
    override fun onEvent(name: String?, params: String?, data: ByteArray?, pffset: Int, length: Int) {
//        Log.i(TAG, String.format("event: name=%s, params=%s", name, params))
        name?.let {
            when(it) {
                SpeechConstant.CALLBACK_EVENT_WAKEUP_READY -> asrListener.wakeupReady()
                SpeechConstant.CALLBACK_EVENT_ASR_BEGIN -> asrListener.asrStartSpeak()
                SpeechConstant.CALLBACK_EVENT_ASR_END -> asrListener.asrStopSpeak()
                else -> {}
            }
        }
        // 去除脏数据
        if (null == params) {
            Log.i(TAG, "params 参数集合为空，停止执行")
            return
        }

        val allJson = JSONObject(params)
        name?.let {
            when(it) {
                // 唤醒事件 唤醒成功
                SpeechConstant.CALLBACK_EVENT_WAKEUP_SUCCESS -> asrListener.wakeupSuc(allJson)
                SpeechConstant.CALLBACK_EVENT_WAKEUP_ERROR -> asrListener.voiceErr("asr 唤醒失败")
                // 引擎就绪，可以说话，一般在收到此事件后通过UI通知用户可以说话了
                SpeechConstant.CALLBACK_EVENT_ASR_FINISH-> asrListener.asrResult(allJson)
                // 一句话的临时结果，最终结果及语义结果
                SpeechConstant.CALLBACK_EVENT_ASR_PARTIAL -> {
                    asrListener.updateUserText(allJson.optString("best_result")) // 用户说的话
                    data?.let {
                        val nlu = JSONObject(String(data, pffset, length))
                        asrListener.nluResult(nlu)
                    }
                }
                else -> {}
            }
        }
    }


    //-------------TTS START------------------
    fun startSpeak(text: String, ttsResultListener: VoiceTTS.OnTTSResultListener? = null) {
        VoiceTTS.startSpeak(text, ttsResultListener)

    }
    // 暂停说话
    fun pauseSpeak() {
        VoiceTTS.pauseSpeak()

    }
    // 重新启动说话
    fun resumeSpeak() {
        VoiceTTS.resumeSpeak()

    }
    // 停止说话
    fun stopSpeak() {
        VoiceTTS.stopSpeak()

    }
    // 释放说话资源
    fun releaseSpeak() {
        VoiceTTS.releaseSpeak()

    }

    // 设置发音人
    fun setPeople(people: String = "0") {
        VoiceTTS.setPeople(people) // "5"度小娇（情感女声）
    }
    // 设置播放音量
    fun setVoiceSpeed(speed: String) {
        VoiceTTS.setVoiceSpeed(speed)
    }
    // 设置播放音语速
    fun setVoiceVolume(volume: String) {
        VoiceTTS.setVoiceVolume(volume)
    }
    //-------------TTS END------------------

    //-------------wakeup start------------------

    fun startWakeup() {
        Log.i(TAG, "开始唤醒 ")
        VoiceWakeup.startWakeup()
    }

    fun stopWakeup() {
        Log.i(TAG, "停止唤醒 ")
        VoiceWakeup.stopWakeup()
    }

    //-------------wakeup start------------------

    //-------------Asr START------------------
    fun startAsr() {
        Log.i(TAG, "开始识别短语音 ")
        VoiceAsr.startAsr()
    }

    fun stopAsr() {
        Log.i(TAG, "停止识别短语音 ")
        VoiceAsr.stopAsr()
    }
    fun cancelAsr() {
        Log.i(TAG, "取消识别短语音 ")
        VoiceAsr.cancelAsr()
    }

    fun releaseAsr() {
        Log.i(TAG, "释放资源识别短语音 ")
        VoiceAsr.releaseAsr(this)
    }
    //-------------Asr START------------------
}